//
//  XTStatusCell.swift
//  MyBlog
//
//  Created by victor on 17/3/23.
//  Copyright © 2017年 victor. All rights reserved.
//

import UIKit

class XTRetweetStatusCell: UITableViewCell {
    
    //MARK:设置属性
    var viewModel : XTStatusViewModel? {
        didSet {
            // 微博文本
            statusLabel.text = viewModel?.status.text
            // 姓名
            nameLabel.text = viewModel?.status.user?.screen_name
            // 设置会员图标 - 直接获取属性，不需要计算
            memberIconView.image = viewModel?.memberIcon
            // 认证图标
            vipIconView.image = viewModel?.vipIcon
            // 用户图像
            iconView.xt_setImage(urlString: viewModel?.status.user?.profile_image_url,
                                 placeholderImage: UIImage(named: "avatar_default_big"),
                                 isAvatar: true)
            
            // 底部工具栏
            toolBar.viewModel = viewModel

            // 被转发的微博正文
            retweetStatusLabel.text = viewModel?.retweetedText
            
            // 测试修改配图视图的高度
//            picView.viewModel = viewModel
        }
    }
    
    /// 颜色分割线
    fileprivate lazy var separatorView : UIView = UIView()
    /// 头像
    fileprivate lazy var iconView = UIImageView()
    /// 姓名
    fileprivate lazy var nameLabel = UILabel()
    /// 会员图标
    fileprivate lazy var memberIconView = UIImageView()
    /// 时间
    fileprivate lazy var timeLabel = UILabel()
    /// 来源
    fileprivate lazy var sourceLabel = UILabel()
    /// 认证图标
    fileprivate lazy var vipIconView = UIImageView()
    /// 微博正文
    fileprivate lazy var statusLabel = UILabel()
    /// 底部工具栏
    fileprivate lazy var toolBar : XTStatusToolBar = XTStatusToolBar()
    /// 配图视图
//    fileprivate lazy var picView : XTStatusPictureView = XTStatusPictureView()
    /// 被转发的微博正文
    fileprivate lazy var retweetStatusLabel = UILabel()
    /// 被转发微博的背景按钮
    fileprivate lazy var retweetStatusBtn = UIButton()
    
    //MARK:设置UI
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        setupUI()
    }
    
    fileprivate func setupUI() {
        separatorView.backgroundColor = UIColor.cz_color(withHex: 0xf2f2f2)
        
        nameLabel.textColor = UIColor.orange
        nameLabel.font = UIFont.systemFont(ofSize: 15)
        
        timeLabel.text = "11:20"
        timeLabel.textColor = UIColor.orange
        timeLabel.font = UIFont.systemFont(ofSize: 10)
        
        sourceLabel.text = "来源"
        sourceLabel.textColor = UIColor.darkGray
        sourceLabel.font = UIFont.systemFont(ofSize: 10)
        
        statusLabel.textColor = UIColor.black
        statusLabel.numberOfLines = 0
        statusLabel.font = UIFont.systemFont(ofSize: 15)
        
        retweetStatusBtn.backgroundColor = UIColor.cz_color(withHex: 0xE3E3E3)
        
        retweetStatusLabel.textColor = UIColor.darkGray
        retweetStatusLabel.numberOfLines = 0
        retweetStatusLabel.font = UIFont.systemFont(ofSize: 14)
        
        toolBar.backgroundColor = UIColor.lightGray
        
        addSubview(separatorView)
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(memberIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(vipIconView)
        addSubview(statusLabel)
        addSubview(retweetStatusBtn)
        addSubview(retweetStatusLabel)
//        addSubview(picView)
        addSubview(toolBar)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        separatorView.snp.makeConstraints { (make) in
            make.top.equalTo(self.snp.top)
            make.left.equalTo(self.snp.left)
            make.right.equalTo(self.snp.right)
            make.height.equalTo(12)
        }
        
        iconView.snp.makeConstraints { (make) in
            make.top.equalTo(24)
            make.left.equalTo(12)
            make.width.equalTo(34)
            make.height.equalTo(34)
        }
        
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(iconView.snp.top).offset(3)
            make.left.equalTo(iconView.snp.right).offset(12)
        }
        
        memberIconView.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp.right).offset(4)
            make.centerY.equalTo(nameLabel.snp.centerY)
        }
        
        timeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp.left)
            make.bottom.equalTo(iconView.snp.bottom)
        }
        
        sourceLabel.snp.makeConstraints { (make) in
            make.left.equalTo(timeLabel.snp.right).offset(8)
            make.centerY.equalTo(timeLabel.snp.centerY)
        }
        
        vipIconView.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconView.snp.right).offset(-4)
            make.centerY.equalTo(iconView.snp.bottom).offset(-4)
        }
        
        statusLabel.snp.makeConstraints { (make) in
            make.top.equalTo(iconView.snp.bottom).offset(12)
            make.left.equalTo(iconView.snp.left)
            make.right.equalTo(self.snp.right).offset(-12)
//            make.bottom.equalTo(retweetStatusBtn.snp.top)
        }
        
        retweetStatusBtn.snp.makeConstraints { (make) in
            make.top.equalTo(statusLabel.snp.bottom).offset(24)
            make.left.equalTo(self.snp.left)
            make.right.equalTo(self.snp.right)
            make.bottom.equalTo(toolBar.snp.top)
            make.height.equalTo(241)
        }
        
        retweetStatusLabel.snp.makeConstraints { (make) in
            make.top.equalTo(retweetStatusBtn.snp.top).offset(12)
            make.left.equalTo(retweetStatusBtn.snp.left).offset(12)
            make.right.equalTo(self.snp.right).offset(-12)
//            make.bottom.equalTo(picView.snp.top)
        }
        
//        picView.snp.makeConstraints { (make) in
//            make.top.equalTo(retweetStatusLabel.snp.bottom).offset(12)
//            make.left.equalTo(retweetStatusLabel.snp.left)
//            make.bottom.equalTo(retweetStatusBtn.snp.top).offset(-12)
//            make.right.equalTo(self.snp.right).offset(-12)
//            make.height.equalTo(385)
//        }
        
        toolBar.snp.makeConstraints { (make) in
            make.left.equalTo(self.snp.left)
            make.bottom.equalTo(self.snp.bottom)
            make.right.equalTo(self.snp.right)
            make.height.equalTo(35)
        }
        
        nameLabel.sizeToFit()
        statusLabel.sizeToFit()
        retweetStatusLabel.sizeToFit()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
